<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta http-equiv="Pragma" content="no-cache">
    <meta http-equiv="Cache-control" content="no-cache">
    <meta http-equiv="Cache" content="no-cache">
    <title>后台管理系统</title>
<#include "/WEB-INF/views/template/bootMainCss.ftl" parse=true encoding="utf-8">
    <link href="${basePath}/bootstrap/css/bootstrap.min.css" rel="stylesheet">
    <link href="${basePath}/bootstrap/css/form.lzy.css" rel="stylesheet">

    <style>
        h1 {
            display: block;
            margin-bottom: 20px;
            border-bottom: 1px solid #999;
            font-size: 16px;
            padding-left: 2px;
            font-weight: bold;
            line-height: 35px;
        }
        #btn{ display: inline-block; background-color: #ffbe34; line-height: 35px; padding: 0 15px; font-size: 14px; color: #fff; border-radius: 4px; cursor: pointer;}
        #depart-box {
            display: block;
            padding: 20px;
        }
        #depart-box .bot {
            display: block;
            padding-top: 50px;
            text-align: center;
        }
        .mytext-box{ width: 660px;}
        .mytext-box input{ float: left; width: 50px; text-align: center; margin: 0 10px 10px 0;}
        .mytext-box .init{float: left; line-height: 34px;}
        .mytext-box small{ display: block; width: 100%; clear: left;}
        .verify-tip{ clear: left;}
    </style>
</head>
<body>

<div class="panel panel-lzy">
    <div class="panel-body">
        <h5 class="tips-lzy">操作提示</h5>
        <ul class="tips-list-lzy">
            <li>◆&nbsp;<span>可以设置按订单量或者订单总额两种方式。</span></li>
            <li>◆&nbsp;<span>开通下级功能是为了销售员在实际销售过程中达到后台所设置的要求时，前台提醒他可以成立团队招聘成员或者获得相应奖励。</span></li>
        </ul>
    </div>
</div>

<form id="Form" class="form-horizontal" method="post">
    <input type="hidden" id="id" name="id" <#if dto??>value="${dto.id!}"</#if>/>
    <div class="panel panel-lzy">
    <div class="panel-body row">
        <h1>● 选择开通下级所需要的条件(单选)</h1>
            <#if dto??>
            <#list conditionList as condition>
                <div class="form-group">
                    <span class="control-label col-sm-2 col-xs-4 detail-span"><span class="text-red"></span>&nbsp;<input class="conditionId" type="radio" name="conditionId" value="${condition.id!}" <#if condition.id==dto.conditionId>checked</#if> />&nbsp;${condition.name!}：第</span>
                    <div class="col-sm-9 col-xs-8 mytext-box myText${condition_index}">
                        <#list numberArr as number>
                            <input type="text" class="form-control" name="numberArr" size="4" maxlength="4" <#if (condition.id!)==(dto.conditionId!)>value="${number!}"</#if>/>
                        </#list>
                        <input class="btn btn-info AddMoreFileBox" type="button" value="+" />
                        <span class="init">${condition.unit!}</span>
                        <span class="verify-tip">请输入大于0的递增整数</span>
                    </div>
                </div>
            </#list>
        <#else>
            <#list conditionList as condition>
                <div class="form-group">
                    <span class="control-label col-sm-2 col-xs-4 detail-span"><span class="text-red"></span>&nbsp;<input class="conditionId" type="radio" name="conditionId" value="${condition.id!}" <#if condition_index == 0>checked</#if> />&nbsp;${condition.name!}：第</span>
                    <div class="col-sm-9 col-xs-8 mytext-box myText${condition_index}">
                        <input type="text" class="form-control" name="numberArr" size="4" maxlength="4" />
                        <input type="text" class="form-control" name="numberArr" size="4" maxlength="4" />
                        <input type="text" class="form-control" name="numberArr" size="4" maxlength="4" />
                        <input type="text" class="form-control" name="numberArr" size="4" maxlength="4" />
                        <input type="text" class="form-control" name="numberArr" size="4" maxlength="4" />
                        <input class="btn btn-info AddMoreFileBox" type="button" value="+" />
                        <span class="init">${condition.unit!}</span>
                        <span class="verify-tip">请输入大于0的递增整数</span>
                    </div>
                </div>
            </#list>
        </#if>
    </div>
</div>

<div class="panel panel-lzy">
    <div class="panel-body row">
        <h1>● 其他设置</h1>
            <input type="hidden" id="_ctx" value="${basePath}"/>
            <input type="hidden" id="id" name="id" <#if dto??>value="${dto.id!}"</#if>/>
            <div class="form-group">
                <span class="control-label col-sm-2 col-xs-4  detail-span"><font
                        color="red">*</font>&nbsp;选择分类：</span>
                <div class="col-sm-9 col-xs-8">
                <#if (dto.classifyId)??>
                    <input id="departName" type="text" class="form-control" size="30" name="departName" value="${dto.classifyName!}"
                           readonly>
                    <input id="departId" type="hidden" name="classifyId" value="${dto.classifyId!}">
                <#else>
                    <input id="departName" type="text" class="form-control" size="30" name="departName" value=""
                           readonly>
                    <input id="departId" type="hidden" name="classifyId" value="">
                </#if>
                    <button type="button" class="btn btn-yellow" id="btn">选择部门</button>
                    <span class="text-warning">请设置该产品对应的分类</span>
                </div>
            </div>
    </div>
</div>
</form>

<div class="form-group">
    <span class="control-label col-sm-4 col-xs-4"></span>
    <div class="col-sm-8 col-xs-8">
    <#if dto??>
        <button type="button" class="btn btn-success" onclick="submitFormEdit();"><i
                class="fa fa-check"></i>提交
       </button>
    <#else>
        <button type="button" class="btn btn-success" onclick="submitForm();"><i
                class="fa fa-check"></i> 提交
        </button>
    </#if>
        <button type="button" class="btn btn-info" onclick="goBack()"><i class="fa fa-close"></i>返回</button>
    </div>
</div>

<div id="depart-box" style="display: none;">
<#if classifyList?? && classifyDto??>
    <#list classifyList as cla>
        <label><input type="radio" name="classifyName" value="${cla.id!}" <#if cla.id == classifyDto.id>checked</#if>> <span>${cla.name!}</span></label>
    </#list>
<#else>
    <#list classifyList as cla>
        <label><input type="radio" name="classifyName" value="${cla.id!}"> <span>${cla.name!}</span></label>
    </#list>
</#if>
    <div class="bot">
        <button type="button" class="btn btn-success">确定</button>
    </div>
</div>

<script src="${basePath}/bootstrap/js/jquery.js" type="text/javascript"></script>
<script src="${basePath}/bootstrap/js/bootstrap.min.js" type="text/javascript"></script>
<script src="${basePath}/bootstrap/plugin/layer/layer.js" type="text/javascript"></script>
<script type="text/javascript" src="https://cdn.bootcss.com/bootstrap-validator/0.5.3/js/bootstrapValidator.min.js"></script>
<script type="text/javascript" src="https://cdn.bootcss.com/bootstrap-validator/0.5.3/js/language/zh_CN.js"></script>
<script type="text/javascript">
    $(window).bootstrapValidator || document.write('<script src="${basePath}/bootstrap/js/bootstrapValidator.min.js"><\/script>'+
            '<script src="${basePath}/bootstrap/js/bootstrapValidator-zh_CN.js"><\/script>');
</script>
<script src="${basePath}/bootstrap/js/common.js" type="text/javascript"></script>
<script type="text/javascript" src="${basePath}/bootstrap/plugin/bootstrap-datetimepicker/bootstrap-datetimepicker.min.js"></script>
<script type="text/javascript" src="${basePath}/bootstrap/plugin/bootstrap-datetimepicker/bootstrap-datetimepicker.zh-CN.js"></script>
<script src="${basePath}/bootstrap/js/campsite.js" type="text/javascript"></script>
<script>
    function goBack(){
        window.location.href = "${basePath}/admin/product/level"
    }
    $(document).ready(function() {
        $(".AddMoreFileBox").each(function(){
            $(this).click(function(){
                if($(this).parent().find("input").length < 31){
                    $(this).before('<input type="text" class="form-control"  name="numberArr" size="3" maxlength="3" />')
                }
            })
        })

    });

    //判断
    function checkInput(){
        var i= 0;
        //判断选中的input
        if($("input[name='conditionId']:checked").val() == "2"){
            i = 1
            $(".myText0 .verify-tip").hide()
        }
        else{
            $(".myText1 .verify-tip").hide()
        }
        var flag = true;
        var dataArr = $(".myText"+i+" input[name='numberArr']");
        var dataTip = $(".myText"+i+" .verify-tip");
        var thisArr = [];
        //循环判断是否为整数
        dataArr.each(function(){
            if(!regInt.test($(this).val())){
                thisArr.push($(this).val());
            }
        })
        //console.log($("input[name='conditionId']:checked").val())
        //循环判断是否递增
        for(var i=0,len=dataArr.length;i<len;i++){
            if(i> 0 && !(parseInt($(dataArr[i]).val()) > parseInt($(dataArr[i-1]).val()))){
                flag = false
            }
        }

        if(flag && thisArr.length == 0){
            dataTip.hide()
        }
        else{
            dataTip.css("display","block")
        }
    }

    //新增
    function submitForm() {
        //执行判断函数
        checkInput();
        var bsVal = $("#Form").data('bootstrapValidator');
        bsVal.validate();
        if($(".verify-tip:visible").length == 0 && bsVal.isValid()){
            var numberArr = [];
            var conditionId = '';
            $(".conditionId").each(function(){
                if($(this).is(":checked")){
                    conditionId = $(this).val();
                    $(this).parent().siblings(".mytext-box").find("input[name='numberArr']").each(function(){
                        if($(this).val() != ""){
                            numberArr.push($(this).val());
                        }
                    })
                }
            })

            $.ajax({
                type: 'POST',
                dataType: 'json',
                url: '${basePath}/admin/product/level/save?conditionId='+conditionId+'&numberArr='+numberArr+'&classifyId='+$("#departId").val(),
                //url: '${basePath}/admin/product/level/save',
                data: {},
                success: function (data) {
                    if (data.code == '0') {
                        layer.alert('新增成功', {
                            icon: 1
                        }, function () {
                            window.location.href = '${basePath}/admin/product/level';
                        })
                    } else {
                        layer.alert(data.desc, {
                            icon: 5
                        });
                    }
                },
                error: function () {
                    layer.alert('系统错误', {
                        icon: 5
                    });
                }
            });
        }
    }

    //编辑
    function submitFormEdit() {
        //执行判断函数
        checkInput();
        var bsVal = $("#Form").data('bootstrapValidator');
        bsVal.validate();
        if($(".verify-tip:visible").length == 0 && bsVal.isValid()){
            var numberArr = [];
            var conditionId = '';
            $(".conditionId").each(function () {
                if ($(this).is(":checked")) {
                    conditionId = $(this).val();
                    $(this).parent().siblings(".mytext-box").find("input[name='numberArr']").each(function () {
                        if ($(this).val() != "") {
                            numberArr.push($(this).val());
                        }
                    })
                }
            })
            $.ajax({
                type: 'POST',
                dataType: 'json',
                url: '${basePath}/admin/product/level/update?conditionId=' + conditionId + '&numberArr=' + numberArr + '&classifyId=' + $("#departId").val() + '&id=' + $("#id").val(),
                data: {},
                success: function (data) {
                    if (data.code == '0') {
                        layer.alert('编辑成功', {
                            icon: 1
                        }, function () {
                            window.location.href = '${basePath}/admin/product/level';
                        })
                    } else {
                        layer.alert(data.desc, {
                            icon: 5
                        });
                    }
                },
                error: function () {
                    layer.alert('系统错误', {
                        icon: 5
                    });
                }
            });
        }
    }

    $(function(){
        $("#btn").click(function () {
            //请求数据成功后 => 弹窗
            //$.ajax();
            var box = $("#depart-box");
            var nameArr = [], idArr = [];
            layer.open({
                type: 1,
                area: ['500px', '400px'],
                title: '产品分类选择',
                content: box,
                success: function () {
                    box.find("button").off("click").click(function () {
                        box.find("label").each(function () {
                            var _this = $(this).find("input");
                            if (_this.is(":checked")) {
                                idArr.push(_this.val());
                                nameArr.push(_this.siblings("span").text());
                            }
                        });
                        //赋值
                        $("#departName").val(nameArr).show().change();
                        $("#departId").val(idArr);
                        layer.closeAll();
                    })
                },
                end: function () {
                    layer.closeAll();
                }
            });
        })
    });

//    表单验证
    $('#Form').bootstrapValidator({
        fields: {
            departName: {
                validators: {
                    notEmpty: {
                        message: '请选择分类'
                    }
                }
            }
//            numberArr: {
//                validators: {
//                    callback: {
//                        message: '请输入递增的整数',
//                        callback: function(value, validator) {
//                            //var items = $('#captchaOperation').html().split(' '), sum = parseInt(items[0]) + parseInt(items[2]);
//                            var flag = true;
//                            //var numberArr = [];
//                            var dataArr = $(".mytext-box input[name='numberArr']");
//                            for(var i=0,len=dataArr.length;i<len;i++){
//                                console.log(dataArr[i].val())
//                                //value = ;
//                                if(!regNumber.test(dataArr[i].val())){
//                                    flag = false
//                                }
//                            }
//                            return flag;
//                        }
//                    }
//                }
//            }
        }
    });

</script>
</body>
</html>
